package com.gticket.orderservice.mapper;

import com.gticket.orderservice.pojo.OrderItems;
import com.gticket.orderservice.pojo.Orders;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: 伴你时光尽头
 * @Date: 2019/11/19 16:56
 */
@Repository
public interface OrderItemMapper {
    /**
     * 通过订单项对象中的fs_id来查找分院id   用于创建订单时
     *
     * @param items
     * @return
     */
    @Select("select cinema_id from ${databasename}.actualseat where as_id = (select as_id from ${databasename}.filmschedule where fs_id = #{fs_id})")
    public Integer selectCinema_idByFs_id(OrderItems items);

    /**
     * 创建订单项
     *
     * @param item
     */
    @Insert("insert into ${databasename}.orderitems(order_id,fs_id,row,col) values(#{order_id},#{fs_id},#{row},#{col})")
    public void addOrderItem(OrderItems item);

    /**
     * 根据order_id查询所有的订单项并封装成List<OrderItems>集合返回
     *
     * @param orders 封装有数据库前缀名和order_id的Order对象
     * @return
     */
    @Select("select * from ${databasename}.orderitems where order_id = #{order_id}")
    public List<OrderItems> getItemsById(Orders orders);

    /**
     * 通过订单项对象中的fs_id来查找分院id   用于创建订单时
     *
     * @param items
     * @return
     */
    @Select("select cinema_name from ${databasename}.cinema where cinema_id = (select cinema_id from ${databasename}.actualseat where as_id = (select as_id from ${databasename}.filmschedule where fs_id = #{fs_id}))")
    public String selectCinema_nameByFs_id(OrderItems items);

    /**
     * 通过fs_id查找movie_name
     *
     * @param items
     * @return
     */
    @Select("select movie_name from ${databasename}.movies where movie_id = (select movie_id from ${databasename}.filmschedule where fs_id = #{fs_id})")
    public String getMovie_nameByFs_id(OrderItems items);

    /**
     * 通过fs_id查询电影开始时间
     *
     * @param item
     * @return
     */
    @Select("select start_time from ${databasename}.filmschedule where fs_id = #{fs_id}")
    public String getStart_timeByFs_id(OrderItems item);

    /**
     * 通过fs_id查找movie_price
     *
     * @param items
     * @return
     */
    @Select("select movie_price from ${databasename}.movies where movie_id = (select movie_id from ${databasename}.filmschedule where fs_id = #{fs_id})")
    public BigDecimal getMovie_priceByFs_id(OrderItems items);

    /**
     * @param item 至少包含databasename和fs_id封装的item对象
     * @return
     */
    @Select("select ch_name from ${databasename}.cinemahall where ch_id = (select ch_id from ${databasename}.actualseat where as_id = (select as_id from ${databasename}.filmschedule where fs_id = #{fs_id}))")
    public String getCH_nameByFs_id(OrderItems item);

    /**
     * 通过fs_id查找开始日期 fs_date
     *
     * @param orderItems
     * @return
     */
    @Select("select fs_date from ${databasename}.filmschedule where fs_id = #{fs_id}")
    public String getFs_dateByFs_id(OrderItems orderItems);

    /**
     * 价格插入
     *
     * @param item
     */
    @Update("update ${databasename}.orderitems set movie_price = #{movie_price} where orderitem_id=#{orderitem_id}")
    public void setMovie_price(OrderItems item);


    /**
     * 根据order_num查询所有的订单项并封装成List<OrderItems>集合返回
     *
     * @param orders 封装有数据库前缀名和order_num的Order对象
     * @return
     */
    @Select("select * from ${databasename}.orderitems where order_id = (select order_id from ${databasename}.orders where order_num=#{order_num}))")
    public List<OrderItems> getItemsByOrder_num(Orders orders);
}
